using System;
using System.Data;

namespace SharpObjects.Data
{

	public class DbRow
	{
		protected DataRow m_dr;

		public DbRow(DataRow dr)
		{
			m_dr = dr;
			m_strprop = new StringProperty(this);
			m_intprop = new IntegerProperty(this);
			m_longprop = new LongProperty(this);
			m_dblprop = new DoubleProperty(this);
			m_dtprop = new DateTimeProperty(this);
			m_boolprop = new BooleanProperty(this);
		}

		public DataRow Row { get { return m_dr; } }

		public bool GetBoolean(string sColumn)
		{
			if (m_dr.IsNull(sColumn))
			{
				throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
			}
			string sValue = m_dr[sColumn].ToString().Trim();
			
			if(sValue == "0")
			{
				return false;
			}
			else if(sValue == "1")
			{
				return true;
			}
			else if(sValue.ToUpper() == "Y")
			{
				return true;
			}
			else if(sValue.ToUpper() == "N")
			{
				return false;
			}
			else if(sValue.ToUpper() == "T")
			{
				return true;
			}
			else if(sValue.ToUpper() == "F")
			{
				return false;
			}
			else
			{
				return Convert.ToBoolean(sValue);
			}
		}
 
		public bool GetBoolean(string sColumn, bool DefaultValue)
		{
			if (m_dr.IsNull(sColumn))
			{
				return DefaultValue;
			}
			return this.GetBoolean(sColumn);
		}

		protected BooleanProperty m_boolprop;
		public BooleanProperty BooleanValue{ get { return m_boolprop; } }

		public DateTime GetDateTime(string sColumn)
		{
			if (m_dr.IsNull(sColumn))
			{
				throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
			}
			return (DateTime) m_dr[sColumn];
		}
 
		public DateTime GetDateTime(string sColumn, DateTime DefaultValue)
		{
			if (m_dr.IsNull(sColumn))
			{
				return DefaultValue;
			}
			return this.GetDateTime(sColumn);
		}

		protected DateTimeProperty m_dtprop; 
		public DateTimeProperty DateTimeValue { get { return m_dtprop; } }
 
		public double GetDouble(string sColumn)
		{
			if (m_dr.IsNull(sColumn))
			{
				throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
			}
			string sValue = m_dr[sColumn].ToString().Trim();
			return Convert.ToDouble(sValue);
		}
 
		public double GetDouble(string sColumn, double DefaultValue)
		{
			if (m_dr.IsNull(sColumn))
			{
				return DefaultValue;
			}
			return this.GetDouble(sColumn);
		}

		protected DoubleProperty m_dblprop;
		public DoubleProperty DoubleValue { get { return m_dblprop; } }

        public decimal GetDecimal(string sColumn)
        {
            if (m_dr.IsNull(sColumn))
            {
                throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
            }
            string sValue = m_dr[sColumn].ToString().Trim();
            return Convert.ToDecimal(sValue);
        }

        public decimal GetDecimal(string sColumn, decimal DefaultValue)
        {
            if (m_dr.IsNull(sColumn))
            {
                return DefaultValue;
            }
            return this.GetDecimal(sColumn);
        }

        protected DecimalProperty m_decprop;
        public DecimalProperty DecimalValue { get { return m_decprop; } }

        public int GetInteger(string sColumn)
		{
			if (m_dr.IsNull(sColumn))
			{
				throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
			}
			string sValue = m_dr[sColumn].ToString().Trim();
			return Convert.ToInt32(sValue);
		}
 
		public int GetInteger(string sColumn, int DefaultValue)
		{
			if (m_dr.IsNull(sColumn))
			{
				return DefaultValue;
			}
			return this.GetInteger(sColumn);
		}

		protected IntegerProperty m_intprop;
		public IntegerProperty IntegerValue { get { return m_intprop; } }
 
		public long GetLong(string sColumn)
		{
			if (m_dr.IsNull(sColumn))
			{
				throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
			}
			string sValue = m_dr[sColumn].ToString().Trim();
			return Convert.ToInt64(sValue);
		}
 
		public long GetLong(string sColumn, long DefaultValue)
		{
			if (m_dr.IsNull(sColumn))
			{
				return DefaultValue;
			}
			return this.GetLong(sColumn);
		}

		protected LongProperty m_longprop;
		public LongProperty LongValue { get { return m_longprop; } }
 
		public string GetString(string sColumn)
		{
			if (m_dr.IsNull(sColumn))
			{
				throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
			}
			return m_dr[sColumn].ToString();
		}
 
		public string GetString(string sColumn, string DefaultValue)
		{
			if (m_dr.IsNull(sColumn))
			{
				return DefaultValue;
			}
			return this.GetString(sColumn);
		}
 
		protected StringProperty m_strprop;
		public StringProperty StringValue { get { return m_strprop; } }

		public class StringProperty
		{
			protected DbRow m_row;
			public StringProperty(DbRow row) { m_row = row; }

			public string this[string sColumn] { get{ return m_row.GetString(sColumn); } }
			public string this[string sColumn, string default_value] { get { return m_row.GetString(sColumn, default_value); } }
		}

		public class IntegerProperty
		{
			protected DbRow m_row;
			public IntegerProperty(DbRow row) { m_row = row; }

			public int this[string sColumn] { get{ return m_row.GetInteger(sColumn); } }
			public int this[string sColumn, int default_value] { get { return m_row.GetInteger(sColumn, default_value); } }
		}

		public class LongProperty
		{
			protected DbRow m_row;
			public LongProperty(DbRow row) { m_row = row; }

			public long this[string sColumn] { get{ return m_row.GetLong(sColumn); } }
			public long this[string sColumn, long default_value] { get { return m_row.GetLong(sColumn, default_value); } }
		}

		public class DoubleProperty
		{
			protected DbRow m_row;
			public DoubleProperty(DbRow row) { m_row = row; }

			public double this[string sColumn] { get{ return m_row.GetDouble(sColumn); } }
			public double this[string sColumn, double default_value] { get { return m_row.GetDouble(sColumn, default_value); } }
		}

        public class DecimalProperty
        {
            protected DbRow m_row;
            public DecimalProperty(DbRow row) { m_row = row; }

            public decimal this[string sColumn] { get { return m_row.GetDecimal(sColumn); } }
            public decimal this[string sColumn, decimal default_value] { get { return m_row.GetDecimal(sColumn, default_value); } }
        }

        public class DateTimeProperty
		{
			protected DbRow m_row;
			public DateTimeProperty(DbRow row) { m_row = row; }

			public DateTime this[string sColumn] { get{ return m_row.GetDateTime(sColumn); } }
			public DateTime this[string sColumn, DateTime default_value] { get { return m_row.GetDateTime(sColumn, default_value); } }
		}

		public class BooleanProperty
		{
			protected DbRow m_row;
			public BooleanProperty(DbRow row) { m_row = row; }

			public bool this[string sColumn] { get{ return m_row.GetBoolean(sColumn); } }
			public bool this[string sColumn, bool default_value] { get { return m_row.GetBoolean(sColumn, default_value); } }
		}

	}




}
